package com.atguigu.gmall.realtime.common.util;

import com.atguigu.gmall.realtime.common.constant.Constant;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

/**
 * @author yhm
 * @create 2023-11-25 11:08
 */
public class HBaseUtil {
    /**
     * 获取连接  没有使用单例
     * @return
     * @throws IOException 获取连接异常
     */
    public static Connection getHBaseConnection() throws IOException {

        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum", Constant.HBASE_ZOOKEEPER_QUORUM);
        return ConnectionFactory.createConnection(conf);
    }

    /**
     * 关闭连接
     * @return
     */
    public static void closeHBaseConnection(Connection con) throws IOException {
        if (con != null && !con.isClosed()){
            con.close();
        }
    }

    /**
     * 创建表格
     * @param con
     * @param namespace
     * @param tableName
     * @param columnFamilies
     * @throws IOException  获取关闭连接异常
     */
    public static void createHBaseTable(Connection con,String namespace,String tableName,String... columnFamilies) throws IOException {

        // 列族为空
        if (columnFamilies == null || columnFamilies.length == 0){
            System.out.println("创建表格最少有一个列族");
            return;
        }

        // 获取admin
        Admin admin = con.getAdmin();
        TableName name = TableName.valueOf(namespace, tableName);

        // 创建表格的异常处理
        try {
            if (admin.tableExists(name)) {
                System.out.println("表格已经存在");
                return;
            }
        }catch (IOException e) {
            e.printStackTrace();
        }

        // 创建表格的建造者

        TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(name);

        // 添加列族信息
        for (String columnFamily : columnFamilies) {
            ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(columnFamily));
            tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());
        }

        // 调用方法创建表格
        try {
            admin.createTable(tableDescriptorBuilder.build());
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 关闭admin
        admin.close();
    }


    /**
     * 删除表格
     * @param con
     * @param namespace
     * @param tableName
     * @throws IOException 获取关闭连接异常
     */
    public static void dropHBaseTable(Connection con,String namespace,String tableName) throws IOException {
        // 获取admin
        Admin admin = con.getAdmin();

        // 调用方法删除表格
        TableName name = TableName.valueOf(namespace, tableName);
        try {
            admin.disableTable(name);
            admin.deleteTable(name);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 关闭连接
        admin.close();


    }
}
